#include "GameOverState.h"
#include "MainMenuState.h"
//#include "GameplayState.h"
#include "../DeadlyShadowsLastHope/Game.h"
#include "GameplayState.h"
#include "LevelState.h"

CGameOverState* CGameOverState::GetInstance()
{
	static CGameOverState s_Instance;
	return &s_Instance;
}

CGameOverState::CGameOverState(void)
{
	m_pD3D	= nullptr;
	m_pTM	= nullptr;
	m_pDI	= nullptr;
	m_pXA	= nullptr;

	m_nBackGroundSfxID		= -1;
	m_nCursorMove			= 1;

	cInitials[3]			= NULL;
	win = true;

	for (unsigned int i = 0; i < 3; ++i)
	{
		cInitials[i]		= 'A';
	}
}

CGameOverState::~CGameOverState(void) { }

void CGameOverState::Enter(void)
{
	//setup the state
	m_pD3D	= CSGD_Direct3D::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();
	m_pDI	= CSGD_DirectInput::GetInstance();
	m_pXA	= CSGD_XAudio2::GetInstance();
	m_pTM	= CSGD_TextureManager::GetInstance();

	m_pFont.Load("../resources/XML/DeadlyShadowsFont.xml");

	//this->m_nBackGroundSfxID = m_pXA->SFXLoadSound(_T("resource/sounds/CJM_SFXBackGround.xwm"));

}

void CGameOverState::Exit(void)
{
	//if (this->m_nBackGroundSfxID != -1)
	//{
	//	this->m_pXA->SFXStopSound(this->m_nBackGroundSfxID);
	//	this->m_pXA->SFXUnloadSound(this->m_nBackGroundSfxID);
	//	this->m_nBackGroundSfxID = -1;
	//}
	//FONTS
}

bool CGameOverState::Input(void)
{
	//-------------------------------------------- SETTING HS---------------------------------

		if (m_pDI->KeyPressed(DIK_RETURN))
		{
			tPlayerInfo		Info;
			strcpy(Info.m_cName, cInitials);
			Info.m_nHighScore = CHighScoresState::GetInstance()->GetHighScore(); //this->m_nHighScore;
			//CHECK for FILE
			if( CHighScoresState::GetInstance()->LoadPlayers(playerInfo, "players.xml") == false )
			{
				CHighScoresState::GetInstance()->GeneratePlayers( playerInfo  );
			}
			//ADD NAME AND SCORE TO VECTOR
			playerInfo.push_back(Info);
			sort(playerInfo.begin(), playerInfo.end(), greater<tPlayerInfo>());

			if (playerInfo.size() > 10)
			{
				playerInfo.pop_back();
			}

			CHighScoresState::GetInstance()->SavePlayers(playerInfo, "players.xml" );

			CGame::GetInstance()->RemoveState(this);
			CGame::GetInstance()->AddState(CHighScoresState::GetInstance());
			//CGame::GetInstance()->RemoveState(CLevelState::GetInstance());
			return true;
		}

		if (m_pDI->KeyPressed(DIK_UP ))
		{
			if (cInitials[letter] == 'Z')
			{
				cInitials[letter]  = 'A' - 1;
			}
			cInitials[letter] ++;
		}
		if (m_pDI->KeyPressed(DIK_DOWN))
		{
			if (cInitials[letter]  == 'A')
			{
				cInitials[letter]  = 'Z' +1;
			}
			cInitials[letter] --;
		}
		if (m_pDI->KeyPressed(DIK_LEFT))
		{
			if (letter == 0)
			{
				letter = 3;
			}
			--letter;
		}
		if (m_pDI->KeyPressed(DIK_RIGHT))
		{
			if (letter == 2)
			{
				letter = -1;
			}
			++letter;
		}
		//CHighScoresState::GetInstance()->SavePlayers(playerInfo , "players.xml" );

	//----------------------------------------------------------------------------------------
	if (this->m_pDI->KeyPressed(DIK_X))
	{
		CGame::GetInstance()->AddState(CMainMenuState::GetInstance());
		CGame::GetInstance()->RemoveState(this);
		return true;
	}
	return true;
}

void CGameOverState::Update(float fElapsedTime)
{
	//this->m_pXA->Update();
}

void CGameOverState::Render(void)
{
	//BACKGROUND
	//this->m_pD3D->GetSprite()->Flush();

	m_pFont.Print("GAME OVER!", int((CGame::GetInstance()->GetWidth() * 0.5) - (strlen("GAME OVER!") * 32)), int(CGame::GetInstance()->GetHeight() * 0.5f), 2.0f, D3DXCOLOR(255,255,255,255));
	for (unsigned int i = 0; i < 3; ++i)
	{
		char z[2] =  { cInitials[i] };
		if (i == letter)
			m_pFont.Print(z, int((CGame::GetInstance()->GetWidth() * 0.5) - (strlen("A") * 32) +(i*64)), int(CGame::GetInstance()->GetHeight() * 0.75f), 2.0f, D3DXCOLOR(255,255,255,255));
		else
			m_pFont.Print(z, int((CGame::GetInstance()->GetWidth() * 0.5) - (strlen("A") * 32)+(i*64)), int(CGame::GetInstance()->GetHeight() * 0.75f), 2.0f, D3DXCOLOR(255,255,0,255));		
	}
}